package com.gkd.springboot_employeeadmin.controller;

import com.gkd.springboot_employeeadmin.pojo.Login;
import com.gkd.springboot_employeeadmin.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;

    @RequestMapping("/user/login")
    public String Login(@PathParam("username") String username,
                        @PathParam("password") String password, Model model, HttpSession session) {
        Login theone = loginService.theone(username);
        if(theone!=null){
        if (theone.getName().equals(username) && theone.getPasswored().equals(password)) {
            session.setAttribute("loginUser", username);
            return "redirect:/main.html";
        }
        else{
            model.addAttribute("msg","用户名或者密码错误");
            return "index";
        }
    }
        else {
            model.addAttribute("msg","用户名或者密码错误");
            return "index";
        }
    }

    @RequestMapping("/user/zhuxiao")
    public String zhuxiao(Model model,HttpSession session)
    {
        session.removeAttribute("loginUser");
        return "index";
    }
}
